package com.mathdroid.gui.main.character;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JToolTip;

import com.mathdroid.gui.ImageLoader;
import com.mathdroid.gui.main.ItemToolTip;
import com.mathdroid.model.Model;
import com.mathdroid.model.enums.EItemSlot;

/**
 * This button is used to open the item enhancement frame.
 */
public final class ItemEnhancementButton extends JButton {

	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The slot of the item.
	 */
	private EItemSlot slot = null;
	
	/**
	 * The item tooltip.
	 */
	private ItemToolTip tip = null;
	
	/**
	 * The image loader.
	 */
	private final ImageLoader il = new ImageLoader();
	
	/**
	 * The horizontal offset of the tooltip.
	 */
	private static final int TOOL_TIP_HORIZONTAL_OFFSET = 20;
	
	/**
	 * Create a JButton with ItemToolTip.
	 * @see ItemToolTip
	 * @param s The item slot type.
	 */
	public ItemEnhancementButton(final EItemSlot s) {
		super();
		
		this.slot = s;
		this.setIcon(this.il.getImage("mods"));
		this.setToolTipText(s + ": Modules");
		
		this.buildMouseListener();
	}

	/**
	 * Add the mouse listener to this button.
	 */
	private void buildMouseListener() {
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(final MouseEvent e) {
				if (Model.getProfile().getEquipedItems().get(slot) != null) {
					ItemEnhancementFrame f = new ItemEnhancementFrame(slot);
					Model.registerObserver(f);
				}
			}
		});
	}
	
	@Override
	public JToolTip createToolTip() {
		if (this.tip == null) {
			this.tip = new ItemToolTip();
			this.tip.setComponent(this);
		}
		return this.tip;
	}
	
	@Override
	public String getToolTipText(final MouseEvent e) {
		String s = this.slot + ": Modules";
		
		return s;
	}
	
	@Override
	public Point getToolTipLocation(final MouseEvent e) {
		Point p = e.getPoint();
		p.x += TOOL_TIP_HORIZONTAL_OFFSET;
		return p;
	 }
}
